package org.demo.tomcat;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author  
 * @date    2010-12-12
 * @file    org.demo.tomcat.StockInfo.java
 */
public class StockInfo extends HttpServlet {
	private Map<String, Stock> map = new HashMap<String, Stock>();
	
	@Override
	public void init() throws ServletException {
		Stock szzs = new Stock(3000.0, 2990.1, "上证指数", "300001");
		Stock pfyh = new Stock(23.22, 23.50, "浦发银行", "000001");
		
		map.put(szzs.getId(), szzs);
		map.put(pfyh.getId(), pfyh);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		double sz = Math.random() * 20;
		double pf = Math.random() * 0.5;
		// 
		boolean flagsz = ((int)(Math.random() * 10)) % 2 == 0;
		boolean flagpf = ((int)(Math.random() * 10)) %2 == 0;
		// 
		Stock szzs = map.get("300001");
		Stock pfyh = map.get("000001");
		double tmp;
		if (flagsz){
			tmp = szzs.getNow() + sz;
		} else {
			tmp = szzs.getNow() - sz;
		}
		tmp = (int)(tmp * 100) / 100.0;
		szzs.setNow(tmp);
		if (flagpf){
			tmp = pfyh.getNow() + pf;
		} else {
			tmp = pfyh.getNow() - pf;
		}
		tmp = (int)(tmp * 100) / 100.0;
		pfyh.setNow(tmp);
		// 
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		// out.println(szzs + "<br/>");
		// out.println(pfyh + "<br/>");
		// 采用 json 回传数据
		StringBuilder sb = new StringBuilder();
		
		// return a json array
//		sb.append("[");
//		sb.append("{id: \"").append(szzs.getId()).append("\"").append(
//				",name:\"").append(szzs.getName()).append("\"").append(
//				",yesterday:\"").append(szzs.getYesterday()).append("\"")
//				.append(",now:\"").append(szzs.getNow()).append("\"").append(
//						"}");
//		sb.append(",");
//		sb.append("{id: \"").append(pfyh.getId()).append("\"").append(
//				",name:\"").append(pfyh.getName()).append("\"").append(
//				",yesterday:\"").append(pfyh.getYesterday()).append("\"")
//				.append(",now:\"").append(pfyh.getNow()).append("\"").append(
//						"}");
//		sb.append("]");
		
		// return a json Object
		sb.append("{");
		sb.append("\"").append(szzs.getId()).append("\":");
		sb.append("{\"id\": \"").append(szzs.getId()).append("\"").append(
				",\"name\":\"").append(szzs.getName()).append("\"").append(
				",\"yesterday\":\"").append(szzs.getYesterday()).append("\"")
				.append(",\"today\":\"").append(szzs.getToday()).append("\"")
				.append(",\"now\":\"").append(szzs.getNow()).append("\"").append(
						"}");
		sb.append(",");
		sb.append("\"").append(pfyh.getId()).append("\":");
		sb.append("{\"id\": \"").append(pfyh.getId()).append("\"").append(
				",\"name\":\"").append(pfyh.getName()).append("\"").append(
				",\"yesterday\":\"").append(pfyh.getYesterday()).append("\"")
				.append(",\"today\":\"").append(pfyh.getToday()).append("\"")
				.append(",\"now\":\"").append(pfyh.getNow()).append("\"").append(
						"}");
		sb.append("}");
		out.print(sb.toString());
		out.flush();
		out.close();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}
